package com.fyzk.activity.down_numhistory;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.bean.model.SsqLiShiJX;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

public abstract class NumHistoryPrizeBase {

	protected Context context;
	
	private View view;
	protected ListView listView;
//	protected NumHistoryAdapter adapter;
	private LinearLayout ll_wuJiLu;
	private TextView tv_wuJiLu;
	private LinearLayout bg_ll;
	private LinearLayout bg_scroll;
	private boolean isWaitting = false;
	private TextView newPrize_tv;
	
	private List<SsqLiShiJX> bellow_lishi = new ArrayList<SsqLiShiJX>();
	
	public NumHistoryPrizeBase(Context context, ArrayList<PrizeHistoryItem> infors){
		this.context = context;
		initView(infors);
	}
	public NumHistoryPrizeBase(Context context, ArrayList<PrizeHistoryItem> infors, boolean isWaitting){
		this.context = context;
		this.isWaitting = isWaitting;
		initView(infors);
	}
	protected boolean isPL3;

	public NumHistoryPrizeBase(Context context, ArrayList<PrizeHistoryItem> infors, boolean isWaitting,boolean isPL3){
		this.context = context;
		this.isWaitting = isWaitting;
		this.isPL3 = isPL3;
		initView(infors);
	}

	private void initView(ArrayList<PrizeHistoryItem> infors){
		view = LayoutInflater.from(context).inflate(R.layout.num_historyprize_view, null);
		newPrize_tv = (TextView) view.findViewById(R.id.num_history_newPrize_tv);
		
		listView = (ListView) view.findViewById(R.id.num_history_listview);
		ll_wuJiLu = (LinearLayout) view.findViewById(R.id.num_history_wujilu);
		tv_wuJiLu = (TextView) view.findViewById(R.id.num_history_wujilu_tv);
		bg_ll = (LinearLayout) view.findViewById(R.id.num_history_ll);
		bg_scroll = (LinearLayout) view.findViewById(R.id.num_history_scrolldown);
		setAdapter(bellow_lishi);
//		listView.setAdapter(adapter);
		
		ll_wuJiLu.setVisibility(View.VISIBLE);
		listView.setVisibility(View.GONE);
		initHistoryList(infors);
		
		if (isWaitting) {
			newPrize_tv.setVisibility(View.VISIBLE);
		}else{
			newPrize_tv.setVisibility(View.GONE);
		}
	}
	
	protected abstract void setAdapter(List<SsqLiShiJX> bellow_lishi);
	protected abstract void setList(List<SsqLiShiJX> bellow_lishi);
	
	/**
	 * 数据转换(显示格式)
	 */
	private void initHistoryList(ArrayList<PrizeHistoryItem> infors1){
		bellow_lishi.clear();
		try {
			for (int i = 0; i < infors1.size(); i++) {
				String issue = infors1.get(i).phase+"期";
				String haoMa = infors1.get(i).wincode;
				String hong = "";
				String lan = "";
				if (haoMa.contains("|")) {
					String[] haoMas = haoMa.split("\\|");
					if (haoMas != null && haoMas.length > 0) {
						
						hong = haoMas[0].replace(",", " ");
						if (haoMas[1].contains(",")) {
							lan = haoMas[1].replace(",", " ");
						}else {
							lan = haoMas[1];
						}
					}else{
//						Toast.makeText(context, "后台服务器数据错误"+haoMa, 3000).show();
					}
				}else {
					hong = haoMa.replace(",", " ");
				}
				SsqLiShiJX liShiJX = new SsqLiShiJX(issue, hong,
						lan);
				bellow_lishi.add(liShiJX);
			}
		} catch (Exception e) {
//			LogUtil.e(e.getMessage());
		}
		
		
		if (bellow_lishi.size() == 0) {
			ll_wuJiLu.setVisibility(View.VISIBLE);
			listView.setVisibility(View.GONE);
		}else{
			ll_wuJiLu.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
//			adapter.setList(bellow_lishi);
			setList(bellow_lishi);
		}
	}
	
	public View getView(){
		return this.view;
	}
	
	
	public void setBg(int resid){
		bg_ll.setBackgroundResource(resid);
	}
	public void setBg_Scr(int resid){
		bg_scroll.setBackgroundResource(resid);
	}
	public void setBg_scrollVisibility(int visibility) {
		bg_scroll.setVisibility(visibility);
	}
	
}
